//
//  VisitorView.swift
//  新浪微博
//
//  Created by 李国伟 on 15/12/6.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
import SnapKit
class VisitorView: UIView {

    //这里面要做的事情，创建两个imageView，一个label和两个button，子控件全部懒加载，并且布局子控件
    func setVisitorInfo(title title: String, imageName: String?)
    {
        messageLabel.text = title
        
        guard let image = imageName else{
            startAnim()
            return
        }
        
        iconView.image = UIImage(named: image)
        homeView.hidden = true
        sendSubviewToBack(maskImageView)
    }
    
    //使用核心动画,可以使用动画组，一边旋转一边放大
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer .addAnimation(anim, forKey: nil)
    }
    
    //重新构造函数
    override  init(frame: CGRect) {
        super.init(frame:frame)
        setUpUI()
    }
    
    //保证纯代码开发，如果出现xib或者storybord就会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        setUpUI()
    }
    
    //MATK： - 懒加载控件
    lazy private var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //mask遮罩
    lazy private var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    lazy private var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示信息,可以在里面设置frame信息
    private lazy var messageLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    //注册按钮
     lazy  var registButton: UIButton = UIButton(backimageName: "common_button_white_disable", title: "注册", color: UIColor.orangeColor())
    
    //登录按钮
     lazy  var longonButton: UIButton = UIButton(backimageName: "common_button_white_disable", title: "登录", color: UIColor.darkGrayColor())
}

//添加子控件，并进行自动布局
extension VisitorView{
    
    //添加到根视图中
    private func setUpUI(){
        addSubview(iconView)
        addSubview(maskImageView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(longonButton)
        
        //1.图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
     
        //2.小房子
        homeView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        //3.文字消息
        messageLabel.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(iconView)
        make.top.equalTo(iconView.snp_bottom).offset(16)
        make.height.equalTo(36)
        make.width.equalTo(224)
        }
       
        //4.注册按钮
        registButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.right.equalTo(iconView.snp_centerX).offset(-20)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }
        
        //5.登录按钮
        longonButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.left.equalTo(iconView.snp_centerX).offset(20)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }

        //6.遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self).multipliedBy(1.0)
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.bottom.equalTo(registButton)
        }
        backgroundColor = UIColor(white: 237.0/255, alpha: 1)
    }
}